/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.converter;

import com.rbh.risk.entity.Patient;
import com.rbh.risk.service.PatientService;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author M
 */
@ManagedBean(name = "patientConverter")
@RequestScoped
@FacesConverter(forClass = Patient.class)
public class PatientConverter implements Converter {

    @ManagedProperty("#{patientService}")
    private PatientService patientService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Patient result = null;
        try {
            result = this.patientService.getById(value);
        } catch (Exception ex) {
        }
        return result;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String result = "";
        try {
            Patient patient = (Patient) value;
            result = patient.getHosGuid();
        } catch (Exception ex) {
        }
        return result;
    }

    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }
}